// pages/member/course/CourseSearch/CourseSearch.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    goodsList: [],
    type: '',
    searchVal: '',

    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    // nav
    navStatus:'',
    priceBol:true,
    salesBol:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.goods_type;
    this.setData({
      type: type
    });
    this._getData();
   
  },
  back(){
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },
  _getData() {
    var that = this;
    this.data.page=1;
    // 初次获取数据
    app._getNetWork({
      url: app.getNetAddresss("plugin.video-demand.api.video-course-goods.get-course-goods&page=1"),
      data: {
        goods_type: that.data.type,
        search: that.data.searchVal,
        filter_order:that.data.navStatus,
        page:that.data.page
      },
      success: function (res) {
        if (res.data.result == 1 && !app._isTextEmpty(res.data.data)) {
          let _data = res.data.data;
          that.setData({
            total_page: _data.last_page,
            goodsList: _data.data
          });
        } else {
          console.error(res);
        }
      },
      fail: function (res) {
        console.error(res);
      }
    });
  },
  _getMoreData() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.video-demand.api.video-course-goods.get-course-goods");
    if (this.data.page >= this.data.total_page) {
      that.setData({
        isLoadMore: false
      });
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      app._getNetWork({
        url: urlStr,
        data: {
          page: that.data.page,
          goods_type: that.data.type,
          search: that.data.searchVal,
          filter_order: that.data.navStatus
        },
        success: function (res) {
          if (res.data.result == 1) {
            let _data = res.data.data;
            that.setData({
              isLoadMore: true,
              goodsList: that.data.goodsList.concat(_data.data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  // 搜索条件
  getDataBth(e){
    let type = e.currentTarget.dataset.type;
    var salesBol = this.data.salesBol,
      priceBol = this.data.priceBol;
    console.log(type);
    if(type){
      if (type == 'sales'){
        this.setData({
          priceBol:true
        });
        if (salesBol){
          this.setData({
            salesBol: false,
            navStatus:'sales_asc'
          });
        }else{
          this.setData({
            salesBol: true,
            navStatus: 'sales_desc'
          });  
        }
      } else if (type == 'price'){
        this.setData({
          salesBol: true
        });
        if (priceBol) {
          this.setData({
            priceBol: false,
            navStatus: 'price_asc'
          });
        } else {
          this.setData({
            priceBol: true,
            navStatus: 'price_desc'
          });
        }
      }
    }else{
      this.setData({
        salesBol: true,
        priceBol: true,
        navStatus:''
      });
    }
    this.setData({
      page:1,
      total_page:0,
      isLoadMore:true,
      goodsList:[]
    });
    this._getData();
  },
  bindKeyInput(e) {
    this.setData({
      searchVal: e.detail.value
    });
 
  },
  toCourse: function (e) {
    let itemid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + itemid
      // url: '/packageA/detail_v2/detail_v2?id=' + itemid
    });
  },
  search(){
    this._getData();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0
    });
    this._getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 加载更多
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
     
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});